<!DOCTYPE html>
<html>
	<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>商品类别</title>
    <link rel="stylesheet" type="text/css"
          href="plugin/jquery-easyui-1.3.3/themes/bootstrap/easyui.css">
    <link rel="stylesheet" type="text/css"
          href="plugin/jquery-easyui-1.3.3/themes/icon.css">
    <script type="text/javascript"
            src="plugin/jquery-easyui-1.3.3/jquery.min.js"></script>
    <script type="text/javascript"
            src="plugin/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
    <script type="text/javascript"
            src="plugin/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" href="plugin/kindeditor/themes/default/default.css"/>
    <script charset="utf-8" src="plugin/kindeditor/kindeditor-all.js"></script>
    <script charset="utf-8" src="plugin/kindeditor/lang/zh_CN.js"></script>

</head>
<body style="margin:1px;margin-top: 5px" id="ff">
<table id="dg" title="" class="easyui-datagrid" pagination="true"
       rownumbers="true" fit="true"
       url="/goodstype/search" toolbar="#tb">
    <thead data-options="frozen:true">
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="10%" align="center" hidden="true">编号</th>
        <th field="typename" width="200" align="center">类别名称</th>
        <th field="typecode" width="150" align="center">类别代码</th>
        <th field="parentid" width="150" align="center" hidden="true">parentid</th>
        <th field="parent" width="150" align="center" formatter="getParentType">父类别</th>
    </tr>
    </thead>
</table>

<div id="tb">
    <div>
        <a href="javascript:openArticleAddDialog()" class="easyui-linkbutton"
           iconCls="icon-add" plain="false">添加</a> 
           <a
            href="javascript:openArticleModifyDialog()"
            class="easyui-linkbutton" iconCls="icon-edit" plain="false">修改</a> 
            <a
            href="javascript:deleteArticle()" class="easyui-linkbutton"
            iconCls="icon-remove" plain="false">删除</a>
    </div>
    
</div>

<div id="dlg" class="easyui-dialog"
     style="width: 870px;height:555px;padding: 10px 20px; position: relative; z-index:1000;"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <table cellspacing="15px">
            <tr>
                <td>类别名称：</td>
                <td><input type="text" id="typename" name="typename"
                           class="easyui-validatebox" required="true"/>&nbsp;<font
                        color="red">*</font>
                    <input id="typeid" name="id" type="hidden" value="0">
                </td>
            </tr>
            <tr>
                <td>类别代码：</td>
                <td><input type="text" id="typecode" name="typecode"/>
                </td>
            </tr>
            <tr>
                <td>父类别</td>
                <td>
                    <select id="parentType" name="parentid">
                        
                    </select>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveArticle()" class="easyui-linkbutton"
       iconCls="icon-ok">保存</a> <a href="javascript:closeArticleDialog()"
                                   class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>


<script type="text/javascript">
    var url = "";

    function deleteArticle() {
        var selectedRows = $("#dg").datagrid('getSelections');
        if (selectedRows.length == 0) {
            $.messager.alert("系统提示", "请选择要删除的数据！");
            return;
        }
        var strIds = [];
        for (var i = 0; i < selectedRows.length; i++) {
            strIds.push(selectedRows[i].id);
        }
        var ids = strIds.join(",");
        $.messager
            .confirm(
                "系统提示",
                "您确认要删除这<font color=red>" + selectedRows.length
                + "</font>条数据吗？",
                function (r) {
                    if (r) {
                        $.ajax({
                            type: "GET",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/goodstype/delete/" + ids,//url
                            data: {},
                            success: function (result) {
                                console.log(result);//打印服务端返回的数据
                                if (result.resultCode == 200) {
                                    $.messager.alert(
                                        "系统提示",
                                        "数据已成功删除！");
                                    $("#dg").datagrid(
                                        "reload");
                                }
                                else {
                                    $.messager.alert(
                                        "系统提示",
                                        "数据删除失败！");
                                }

                                ;
                            },
                            error: function () {
                                $.messager.alert("ERROR！");
                            }
                        });
                    }
                });
    }

    function openArticleAddDialog() {
        $("#dlg").dialog("open").dialog("setTitle", "添加商品类别");
        $("#parentType").append(`<option value="0">父类别</option>`);
        //请求所有的父类别
        $.ajax({
            url:"/goodstype/allParentType",
            type:"GET",
            async : false,
            dataType: "json",
            success:function(result){
                 if(result.resultCode==200){
                     for(var i=0;i<result.data.length;i++){
                         $("#parentType").append(`<option value="${result.data[i].id}">${result.data[i].typename}</option>`);
                     }
                 }
            }
        })
        url="/goodstype/insert";
    }

    function saveArticle() {
        var typename = $("#typename").val();
        var id = $("#typeid").val();
        var typecode = $("#typecode").val();
        var parentid = $("#parentType").val();
        var data = {"id":id,"typename": typename, "typecode": typecode, "parentid": parentid};
        $.ajax({
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: url,//url
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            success: function (result) {
                console.log(result);//打印服务端返回的数据
                if (result.resultCode == 200) {
                    $.messager.alert("系统提示", "保存成功");
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                    resetValue();
                }
                else {
                    $.messager.alert("系统提示", "操作失败");
                    $("#dlg").dialog("close");
                    resetValue();
                }
            },
            error: function () {
                $.messager.alert("系统提示", "操作失败");
            }
        });
    }

    function openArticleModifyDialog() {
        var selectedRows = $("#dg").datagrid('getSelections');
        if (selectedRows.length != 1) {
            $.messager.alert("系统提示", "请选择一条要编辑的数据！");
            return;
        }
        var row = selectedRows[0];
        $("#dlg").dialog("open").dialog("setTitle", "修改信息");
        $('#fm').form('load', row);
        $("#typeid").val(row.id);
        $("#parentType").append(`<option value="0">父类别</option>`);
        
        //请求所有的父类别
        $.ajax({
            url:"/goodstype/allParentType",
            type:"GET",
            async : false,
            dataType: "json",
            success:function(result){
                 if(result.resultCode==200){
                	 for(var i=0;i<result.data.length;i++){
                		 if(result.data[i].id==row.parentid){
                			 $("#parentType").append(`<option value="${result.data[i].id}" selected="selected">${result.data[i].typename}</option>`);
                		 }else{
                			 $("#parentType").append(`<option value="${result.data[i].id}">${result.data[i].typename}</option>`);
                		 }
                	 }
                 }
            }
        })
        url="/goodstype/update";
    }

    function getParentType(val, row) {
    	var parentName="";
    	$.ajax({
    		url:"/goodstype/info/"+row.parentid,
    		type:"GET",
    		async : false,
    		dataType: "json",
    		success:function(result){
    			 if(result.resultCode==200){
	    			 parentName=result.data.typename;
    			 }
    		}
    	})
    	return '<span>'+parentName+'</span>';
    	
    }

    function resetValue() {
        $("#typename").val("");
        $("#typecode").val("");
        $("#parentType").children().remove();
    }

    function closeArticleDialog() {
        $("#dlg").dialog("close");
        resetValue();
    }
</script>
</body>
</html>
